﻿using System;
using System.Xml.Linq;
using DotNetApp.Toolkit.Requests;
using WinSource.Model;

namespace WinSource.Client
{
    internal class GetArticlesFromApiRequest : Request<GetArticlesRequestArgs>
    {
        #region Constants

        private const string Url = "http://winsource.com/xmlrpc.php";

        #endregion

        #region Constructor

        public GetArticlesFromApiRequest(Action<GetArticlesRequestArgs> callback = null)
            : base(callback)
        {
        }

        #endregion

        #region Properties

        protected override string Link
        {
            get { return Url; }
        }

        protected override string Post
        {
            get
            {
                return
                    "<?xml version='1.0' ?><methodCall><methodName>NeverstillPosts</methodName><params><param><value><i4>0</i4></value></param><param><value><i4>20</i4></value></param></params></methodCall>";
            }
        }

        #endregion

        #region Request Overrides

        protected override void OnRequestCompleted(RequestResponseEventArgs requestResponseEventArgs)
        {
            if (!requestResponseEventArgs.Cancelled)
            {
                GetArticlesRequestArgs getArticlesRequestArgs = new GetArticlesRequestArgs(requestResponseEventArgs.Error == null, ResponseTimeInMs);

                if (requestResponseEventArgs.Error == null)
                {
                    try
                    {
                        XDocument xDocument = XDocument.Parse(requestResponseEventArgs.Result);
                        XElement methodResponseElement = xDocument.Element("methodResponse");
                        XElement paramsElement = methodResponseElement.Element("params");
                        XElement paramElement = paramsElement.Element("param");
                        XElement valueElement = paramElement.Element("value");
                        XElement structElement = valueElement.Element("struct");

                        foreach (XElement memberElement in structElement.Elements("member"))
                        {
                            if (GetElementValue(memberElement, "name") == "posts")
                            {
                                XElement valueElement1 = memberElement.Element("value");
                                XElement arrayElement = valueElement1.Element("array");
                                XElement dataElement = arrayElement.Element("data");

                                foreach (XElement valueElement2 in dataElement.Elements("value"))
                                {
                                    XElement structElement2 = valueElement2.Element("struct");

                                    string id = string.Empty;
                                    string thumbnail = string.Empty;

                                    foreach (XElement subMemberElement in structElement2.Elements("member"))
                                    {
                                        switch (GetElementValue(subMemberElement, "name"))
                                        {
                                            case "ID":
                                                id = subMemberElement.Element("value").Value;
                                                break;

                                            case "post_thumb":
                                                thumbnail = subMemberElement.Element("value").Value;
                                                break;
                                        }

                                        if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(thumbnail))
                                        {
                                            break;
                                        }
                                    }

                                    if (!string.IsNullOrEmpty(id))
                                    {
                                        getArticlesRequestArgs.Articles.Add(new Article(id, thumbnail));
                                    }
                                }
                            }
                        }
                    }
                    catch
                    {
                        getArticlesRequestArgs.IsSuccess = false;
                    }

                    InvokeCallback(getArticlesRequestArgs);
                }
            }
        }

        #endregion
    }
}